﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.Entities;
using OnlineVideos.Common;
using OnlineVideos.Data;
using Common.Library;
using OnlineVideos.View_Models;
using OnlineVideos.UI;
using System.ComponentModel;
using OnlineVideos.Utilities;
using Common.Utilities;
using System.Threading.Tasks;


namespace OnlineVideos
{
    public partial class QuizResultReview : PhoneApplicationPage
    {
        #region GlobalDeclaration
        List<QuizQuestions> question = null;
        QuizExitPopUp Quizpopup;
        int popupdisplay = 0;
        #endregion

        public QuizResultReview()
        {
            try
            {
                InitializeComponent();
                Quizpopup = new QuizExitPopUp();
                if (AppSettings.ShowID != "0")
                {
                    LoadPivotThemes(AppSettings.ShowUniqueID);
                }
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in QuizResultReview Method In QuizResultReview.cs file.", ex);
            }
        }
        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(LayoutRoot, adstackpl, 4);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In SongDetails file", ex);
                string excepmess = "Exception in LoadAds Method In SongDetails file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }

        }  
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                LoadAds();
                FlurryWP8SDK.Api.LogEvent("QuizResultReview Page", true);
                AppSettings.ShowQuestionId = "1";
                GetPageDataInBackground();
                PageHelper.RemoveEntryFromBackStack("QuizResultReview");
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In QuizResultReview.cs file.", ex);
            }
        }

        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                        (object s, DoWorkEventArgs a) =>
                                        {
                                            a.Result = QuizManager.GetresultReviewdatawp8();
                                        },
                                        (object s, RunWorkerCompletedEventArgs a) =>
                                        {
                                            question = (List<QuizQuestions>)a.Result;
                                            int showid=AppSettings.ShowUniqueID;
                                            int quizno = Convert.ToInt32(AppSettings.ShowQuizId);
                                            if (Task.Run(async () => await Constants.connection.Table<QuizQuestions>().Where(i => i.ShowID == showid && i.QuizNo==quizno).ToListAsync()).Result.Count == 1)
                                            {
                                                imgprevious.Visibility = Visibility.Collapsed;
                                               imgnext.Visibility = Visibility.Collapsed;
                                            }
                                        
                                            spQuizresult.DataContext = question[0];
                                            QuizHelper.SetQuizOptionColor(question[0].QuestionType, question[0].UserAnswer, question[0].Answer, "A", txtoption1, txtuserans, txtcorrectans, useransgrid, correctansgrid, stkcorrectans);
                                            QuizHelper.SetQuizOptionColor(question[0].QuestionType, question[0].UserAnswer, question[0].Answer, "B", txtoption2, txtuserans, txtcorrectans, useransgrid, correctansgrid, stkcorrectans);
                                            QuizHelper.SetQuizOptionColor(question[0].QuestionType, question[0].UserAnswer, question[0].Answer, "C", txtoption3, txtuserans, txtcorrectans, useransgrid, correctansgrid, stkcorrectans);
                                            QuizHelper.SetQuizOptionColor(question[0].QuestionType, question[0].UserAnswer, question[0].Answer, "D", txtoption4, txtuserans, txtcorrectans, useransgrid, correctansgrid, stkcorrectans);
                                        }
                                      );
            bwHelper.RunBackgroundWorkers();
        }       
        private void LoadPivotThemes(long ShowID)
        {
            try
            {
                tblkVideosTitle.Text = QuizManager.GetSubjectsTitle();
            }
            catch (Exception ex)
            {
                
                   Exceptions.SaveOrSendExceptions("Exception in LoadPivotThemes Method In QuizResultReview.cs file.", ex);
            }
        }

        private void Imgnxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                //if(AppSettings.ShowMaxQuestionId=="1")
                //    imgnext.Visibility = Visibility.Collapsed;
                int maxid = (Convert.ToInt32(AppSettings.ShowMaxQuestionId) - 1);
                if (AppSettings.ShowQuestionId == maxid.ToString())
                {
                    imgnext.Visibility = Visibility.Collapsed;
                }
                AppSettings.ShowQuestionId = (Convert.ToInt32(AppSettings.ShowQuestionId) + 1).ToString();
                imgprevious.Visibility = Visibility.Visible;
                imgnext.Margin = new Thickness(5, 0, 0, 0);
                imgexit.Margin = new Thickness(5, 0, 0, 0);
                GetPageDataInBackground();
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in Imgnxt_MouseLeftButtonDown Method In QuizResultReview.cs file.", ex);
            }
        }

        private void Imgprvs_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                int id = (Convert.ToInt32(AppSettings.ShowQuestionId) - 1);
                int maxid = (Convert.ToInt32(AppSettings.ShowMaxQuestionId) - 1);
                AppSettings.ShowQuestionId = id.ToString();
                if (AppSettings.ShowQuestionId == "1")
                {
                    imgprevious.Visibility = Visibility.Collapsed;
                }
                if (AppSettings.ShowQuestionId == maxid.ToString())
                {
                    imgnext.Visibility = Visibility.Visible;
                    imgnext.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("/Images/Next.png");
                }
                GetPageDataInBackground();
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in Imgprvs_MouseLeftButtonDown Method In QuizResultReview.cs file.", ex);
            }
        }

        private void imgexit_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                PageHelper.RemoveEntryFromBackStack("MainPage");
                NavigationService.Navigate(NavigationHelper.getQuizList(AppSettings.ShowID, tblkVideosTitle.Text));
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in imgexit_MouseLeftButtonDown Method In QuizResultReview.cs file.", ex);
            }
        }

        private void imgTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (Quizpopup.popMessage.IsOpen == false)
                {
                    Quizpopup.Show();
                    popupdisplay = 1;
                    e.Cancel = true;
                    base.OnBackKeyPress(e);
                }
                else
                {
                    Quizpopup.Close();
                }
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_BackKeyPress Method In QuizResultReview.cs file.", ex);
            }
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("QuizResultReview Page");
                PageHelper.RemoveEntryFromBackStack("QuizResultReview");
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In QuizResultReview.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In QuizResultReview.cs file.", ex);
            }
        }
    }
}